package com.fish.yssb.ui.page.util;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Base64;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 安卓端AES加密
 */
public class LcndUtil {

    public static int readFile(Context c ,String filename ) {
        char[] a = new char[32*1024*1024];
        AssetManager manager = c.getResources().getAssets();
        try {
            InputStream inputStream = manager.open(filename);
            InputStreamReader isr = new InputStreamReader(inputStream , "GB2312" );
            BufferedReader br = new BufferedReader(isr);
            int length;
            length = br.read(a,0,32*1024*1024);
            //关流
            br.close();
            isr.close();
            inputStream.close();
            return length;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String readAsserts(Context c ,String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    c.getResources().getAssets().open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static boolean isRightfulTXT(File f) {

        String regexp ="[^\\x00-\\xff]";//双字节字符
        Pattern p = Pattern.compile(regexp);

        try{
            FileInputStream fis =new FileInputStream(f);
            //"GBK"编码方式支持双字节字符
            InputStreamReader isr =new InputStreamReader(fis,"ASCII");
            BufferedReader br =new BufferedReader(isr);
            String line ="";
            while((line = br.readLine()) != null){
                //循环读取文件每一行，检测是否含有双字节字符
                Matcher m = p.matcher(line);
                if(m.find()) {
                    fis.close();
                    isr.close();
                    br.close();
                    return false;
                }
            }
            fis.close();
            isr.close();
            br.close();
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }

        return true;
    }

}


